package com.lmt.util;

import com.lmt.entity.UserInfo;
import com.lmt.base.BaseEntity;
import com.lmt.base.BaseSearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * Redis缓存工具
 * Date 2018/07/17
 *
 * @author MT-Lin
 */
@SuppressWarnings("ALL")
@Component
public class RedisCacheUtil {

    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String,Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private Logger logger = LoggerFactory.getLogger(RedisCacheUtil.class);

    /**
     * 将用户信息缓存到Redis
     * @param userInfo 需要缓存的用户信息
     */
    public void cacheUserInfo(UserInfo userInfo) {
        ValueOperations<String, Object> userInfoValueOperations = redisTemplate.opsForValue();
        userInfoValueOperations.set(userInfo.getTokenId(), userInfo);
    }

    /**
     * 从Redis缓存中获取用户信息
     * @param tokenId 用户凭证
     * @return 通过tokenId获取的用户信息
     */
    public UserInfo getUserInfoFromCache(String tokenId) {
        ValueOperations<String, Object> userInfoValueOperations = redisTemplate.opsForValue();
        return (UserInfo) userInfoValueOperations.get(tokenId);
    }

    /**
     * 从Redis缓存中获取用户名
     * @param baseEntity BaseEntity
     * @return 用户名
     */
    public String getUserName(BaseEntity baseEntity) {
        UserInfo userInfo = this.getUserInfoFromCache(baseEntity.getTokenId());
        if (null == userInfo) {
            return null;
        }
        return userInfo.getUserName();
    }

    /**
     * 从Redis缓存中获取用户名
     * @param tokenId tokenId
     * @return 用户名
     */
    public String getUserName(String tokenId) {
        UserInfo userInfo = this.getUserInfoFromCache(tokenId);
        if (null == userInfo) {
            return null;
        }
        return userInfo.getUserName();
    }

    /**
     * 获取用户ID
     * @param object tokenId或者BaseEntity
     * @return 用户ID或null
     */
    public String getUserId(Object object) {
        UserInfo userInfo = null;
        if (object instanceof String) {
            userInfo = this.getUserInfoFromCache((String) object);
        }
        if (object instanceof BaseEntity) {
            userInfo = this.getUserInfoFromCache(((BaseEntity) object).getTokenId());
        }
        if (object instanceof BaseSearch) {
            userInfo = this.getUserInfoFromCache(((BaseSearch) object).getTokenId());
        }
        if (null == userInfo) {
            return null;
        }
        return userInfo.getId();
    }
}
